/*
 * Copyright 2011 Stefan Ferstl
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.debmavenplugin.archiver.targz;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;

import com.google.code.debmavenplugin.archiver.AbstractArchiver;
import com.google.code.debmavenplugin.archiver.AbstractCollectingFileFilter;
import com.google.code.debmavenplugin.archiver.ArchiveMetaData;


/**
 * Archiver to write &quot;tar.gz&quot; archives.
 */
public class TarGzArchiver extends AbstractArchiver<FileTarArchiveEntry> {

  private final boolean useRelativePaths;

  /**
   * Constructor.
   *
   * @param target The target file of this archiver.
   * @param metaData The metadata of the archive to be created.
   * @param useRelativePaths <code>true</code> to use relative paths in the
   * archive, <code>false</code> else.
   */
  public TarGzArchiver(File target, ArchiveMetaData metaData, boolean useRelativePaths) {
    super(target, metaData);
    this.useRelativePaths = useRelativePaths;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected AbstractCollectingFileFilter<FileTarArchiveEntry> createFileFilter(ArchiveMetaData metaData) {
    return new TarArchiveCollectionFileFilter(metaData, this.useRelativePaths);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected TarArchiveOutputStream createArchiveOutputStream(File file) throws IOException {
    return new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(file)));
  }
}
